package com.guigu.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.erp.dto.DesignModuleDto;
import com.guigu.erp.pojo.DesignFile;
import com.guigu.erp.pojo.DesignModule;
import com.guigu.erp.pojo.DesignModuleDetails;
import com.guigu.erp.service.DesignFileService;
import com.guigu.erp.service.DesignModuleDetailsService;
import com.guigu.erp.service.DesignModuleService;
import com.guigu.erp.utils.NumberUtils;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.DesignFileVo;
import com.guigu.erp.vo.ModuleFileVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

/**
 * ClassName:DesignModuleController
 * Package:com.guigu.erp.controller
 * Description:
 *
 * @Date:2024/2/28 20:57
 * @Author:chenchong
 */
@CrossOrigin
@Controller
@RequestMapping("/design/module")
public class DesignModuleController {

    @Autowired
    private NumberUtils numberUtils;
    @Autowired
    private DesignFileService designFileService;
    @Autowired
    private DesignModuleService designModuleService;
    @Autowired
    private DesignModuleDetailsService designModuleDetailsService;

    //处理制定物料设计单的确认按钮请求
    @RequestMapping("/add")
    @ResponseBody
    public String add(@RequestBody DesignModuleDto designModuleDto) {
        //生成物料设计单编号
        String typeNumber = numberUtils.getTypeNumber("200");
        designModuleDto.setDesignId(typeNumber);
        //产品物料的总成本
        List<DesignModuleDetails> details = designModuleDto.getDetails();
        BigDecimal now = new BigDecimal(0);

        for (int i = 0; i < details.size(); i++) {
            BigDecimal w = details.get(i).getSubtotal();
            now = now.add(w);
        }
        //赋值
        designModuleDto.setCostPriceSum(now);

        //设置审核标志 S001-0 等待审核
        designModuleDto.setCheckTag("S001-0");

        //设置变更标志 B002-0 未变更
        designModuleDto.setChangeTag("B002-0");

        //设置当前物料设计单的分类编号
        //获取产品档案编号
        String productId = designModuleDto.getProductId();

        //根据产品档案编号查询分类级别
        QueryWrapper qw = new QueryWrapper();
        qw.eq("product_id", productId);
        DesignFile one = designFileService.getOne(qw);
        if (one != null) {
            designModuleDto.setFirstKindId(one.getFirstKindId());
            designModuleDto.setFirstKindName(one.getFirstKindName());
            designModuleDto.setSecondKindId(one.getSecondKindId());
            designModuleDto.setSecondKindName(one.getFirstKindName());
            designModuleDto.setThirdKindId(one.getThirdKindId());
            designModuleDto.setThirdKindName(one.getThirdKindName());
        }

        designModuleService.makeDesignModule(designModuleDto);
        return "ok";
    }

    /**
     * 查询未审核的产品物料表所有信息
     */
    @ResponseBody
    @RequestMapping("/showReviewed")
    public PageUtils<DesignModule> updateReviewed() {
        PageUtils<DesignModule> show = designModuleService.show();
        return show;

    }

    /**
     * 查看信息
     */
    @ResponseBody
    @RequestMapping("/showDetails")
    public PageUtils<DesignModuleDetails> showDetails(int id) {
        PageUtils<DesignModuleDetails> designModuleDetailsPageUtils = designModuleDetailsService.selectId(id);
        return designModuleDetailsPageUtils;
    }

    /**
     * 查看信息
     */
    @ResponseBody
    @RequestMapping("/showDetails1")
    public List<DesignModuleDetails> showDetails1(int id) {
        System.out.println("id"+id);
        QueryWrapper<DesignModuleDetails> qw=new QueryWrapper<>();
        qw.eq("PARENT_ID",id);
        List<DesignModuleDetails> list = designModuleDetailsService.list(qw);
        return list;
    }
    /**
     * 查看信息
     */
    @ResponseBody
    @RequestMapping("/moduleId")
    public DesignModule moduleId(String id) {

        QueryWrapper<DesignModule> qw=new QueryWrapper<>();
        qw.eq("PRODUCT_ID",id);
        DesignModule one = designModuleService.getOne(qw);
        return one;
    }
    /**
     * 查看信息
     */
    @ResponseBody
    @RequestMapping("/show")
    public DesignModule show(int id) {
        QueryWrapper<DesignModule> qw = new QueryWrapper<DesignModule>();
        qw.eq("ID", id);
        DesignModule one = designModuleService.getOne(qw);
        return one;
    }

    /**
     * 是否同意通过物料审核
     */
    @ResponseBody
    @RequestMapping("/update")
    public boolean update(@RequestBody DesignModule designModule) {

        boolean b = designModuleService.updateById(designModule);
        return b;
    }

    /**
     * 查看物料表
     */
    @ResponseBody
    @RequestMapping("/show111")
    public PageUtils<DesignModule> show111(ModuleFileVo vo, int pageNo, int pageSize) {
        PageUtils<DesignModule> designModulePageUtils = designModuleService.moduleShow(vo, pageNo, pageSize);

        return designModulePageUtils;
    }

    /**
     * 查询所有物料不重复的名字
     */
    @ResponseBody
    @RequestMapping("/commodity")
    public List<DesignFile> commodity() {
        List<DesignFile> commodity = designModuleService.commodity();
        return commodity;
    }


    /**
     * 查询可用数量是否用完
     */
    @Transactional
    @ResponseBody
    @RequestMapping("/residualAmount")
    public List<DesignModuleDetails> residualAmount(String productId) {
      QueryWrapper<DesignModule> qw=new  QueryWrapper<>();
        qw.eq("PRODUCT_ID",productId);
        DesignModule one = designModuleService.getOne(qw);
        QueryWrapper<DesignModuleDetails> qw1=new  QueryWrapper<>();
        qw1.eq("PARENT_ID",one.getId());
        List<DesignModuleDetails> list = designModuleDetailsService.list(qw1);
        return list;
    }
}
